package com.gdk.ksoft.ui.reporters.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gdk.ksoft.R;
import com.gdk.ksoft.data.common.SystemData;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Mrper on 2015/1/23.
 * 商品列表数据适配器
 */
public class GoodsAdapter  extends BaseAdapter{

    private Context context;
    private List<HashMap<String,Object>> goodslist;

    public GoodsAdapter(Context context,List<HashMap<String,Object>> goodslist){
        this.context = context;
        this.goodslist = goodslist;
    }

    @Override
    public int getCount() {
        return goodslist==null?0:goodslist.size();
    }

    @Override
    public Object getItem(int position) {
        return goodslist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HashMap<String, Object> item = goodslist.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_pplaceorder_lstorder, null);
            holder.imgPic = (ImageView) convertView.findViewById(R.id.item_pplaceorder_lstorder_imgIcon);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.item_pplaceorder_lstorder_txtTitle);
            holder.txtSmallTitle = (TextView) convertView.findViewById(R.id.item_pplaceorder_lstorder_txtSmallTitle);
            holder.txtContent = (TextView) convertView.findViewById(R.id.item_pplaceorder_lstorder_txtContext);
            holder.txtSmallContent = (TextView) convertView.findViewById(R.id.item_pplaceorder_lstorder_txtSmallContext);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        new com.lidroid.xutils.BitmapUtils(context)
                .configDefaultLoadFailedImage(R.drawable.ic_default_goods)
                .configDefaultLoadingImage(R.drawable.ic_default_goods)
                .display(holder.imgPic, SystemData.BASE_URL + item.get("sp_pic"));
        holder.txtTitle.setText(item.get("sp_mc").toString());
        holder.txtSmallTitle.setText(item.get("sp_gg").toString());
        holder.txtContent.setText(item.get("sp_tm").toString());
        int Js = Integer.parseInt(item.get("js").toString());
        int Ss = Integer.parseInt(item.get("ss").toString());
        holder.txtSmallContent.setText((Js == 0 ? "" : Js + "件") + (Ss == 0 ? "" : Ss + "散"));
        return convertView;
    }

    /**
     * 获取数据源
     * @return
     */
    public List<HashMap<String,Object>> getDataSource(){
        return goodslist;
    }

    class ViewHolder{
        public ImageView imgPic;
        public TextView txtTitle;
        public TextView txtSmallTitle;
        public TextView txtContent;
        public TextView txtSmallContent;
    }

}
